﻿using Android.Widget;
using CoachingOrganizer.Logic.Models.Tools.MoneyCalculation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoachingOrganizer.Android.Adapters.Tools.MoneyCalculation
{
    public class MoneyFlowItemNameSuggestionsFilter : Filter
    {
        private readonly MoneyFlowItemNameSuggestionsAdapter _adapter;

        public MoneyFlowItemNameSuggestionsFilter(MoneyFlowItemNameSuggestionsAdapter adapter)
        {
            _adapter = adapter;
        }

        protected override Filter.FilterResults PerformFiltering(Java.Lang.ICharSequence constraint)
        {
            string strConstraint = constraint == null ? null : constraint.ToString();
            MoneyFlowItemNameSuggestionModel[] items = strConstraint == null ? _adapter.Items.ToArray() : _adapter.Items.Where(i => i.SuggestedName.Contains(strConstraint)).ToArray();
            FilterResults results = new FilterResults()
            {
                Values = items.Select(i => i.SuggestedName).ToArray(),
                Count = items.Length
            };
            return results;
        }

        protected override void PublishResults(Java.Lang.ICharSequence constraint, Filter.FilterResults results)
        {
            _adapter.SetFilter(results.Values.ToArray<string>());
        }
    }
}
